package com.ifast.common.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.utils.DicSet;
import com.ifast.common.utils.DicUtil;
import com.ifast.common.utils.HttpContextUtils;
import com.ifast.common.utils.Item;
import com.ifast.common.utils.ShiroUtils;
import com.ifast.sys.domain.UnitBasicinfoDO;
import com.ifast.sys.domain.UserDO;
import com.ifast.sys.service.UnitBasicinfoService;
import com.ifast.sys.util.PermissUtil;
import com.ifast.sys.util.PrivContent;
import com.ifast.sys.util.UnitUtil;

/**
 * 
 * <pre>
 * </pre>
 * 
 * <small> 2018年2月25日 | Aron</small>
 */
public abstract class AdminBaseController {

    protected Logger log = LoggerFactory.getLogger(this.getClass());


    public UserDO getUser() {
        return ShiroUtils.getSysUser();
    }
    public Long getUserId() {
        return getUser().getId();
    }

    public Long getUnitId(){
    	return ShiroUtils.getSysUser().getUnitid();
    }
    
    public UnitBasicinfoDO getUnit(){
    	return ShiroUtils.getSysUser().getUnit();
    }
    
    public static Subject getSubjct() {
        return SecurityUtils.getSubject();
    }

    public String getUsername() {
        return getUser().getUsername();
    }

    /**
     * <pre>
     * 自动获取分页参数，返回分页对象page
     * </pre>
     * 
     * <small> 2018年4月15日 | Aron</small>
     * 
     * @param e
     * @return
     */
    public <E> Page<E> getPage(Class<E> e) {
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        return new Page<>(pageNumber, pageSize);
    }

    private int getParaToInt(String key, int defalut) {
        String pageNumber = HttpContextUtils.getHttpServletRequest().getParameter(key);
        if (StringUtils.isBlank(pageNumber)) {
            return defalut;
        }
        return Integer.parseInt(pageNumber);
    }


    public void sendJson(HttpServletResponse response, StringBuffer str) {
        PrintWriter out = null;
        try {
            response.setContentType("application/json; charset=utf-8");
            response.setHeader("Cache-Control", "no-cache");
            out = response.getWriter();
            out.print(str);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    /**
     * 发送json格式数据
     *
     * @param str
     */
    public void sendJson(HttpServletResponse response, String str) {
        PrintWriter out = null;
        try {
            response.setContentType("application/json; charset=utf-8");
            response.setHeader("Cache-Control", "no-cache");

            out = response.getWriter();
            out.print(str);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    /**
     * 发送HTMl格式文本
     *
     * @param str
     */
    public void sendHtml(HttpServletResponse response, String str) {
        PrintWriter out = null;
        try {
            response.setContentType("text/html; charset=utf-8");
            response.setHeader("Cache-Control", "no-cache");
            out = response.getWriter();
            out.print(str);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    //根据用户权限取得对应管辖范围
    public Map  getSsqxMapByUserPriv(){
        UserDO user = getUser();
        Map resultMap = new HashMap();
        List<Item> ssqxlist = DicSet.get(DicUtil.SCHOOL.SCHOOL_SSQX);
        Map ssqxMap = new LinkedHashMap();
        //根据当前用户权限获取区县范围
        if(PermissUtil.hasPermission(PrivContent.SM_SJPRIV)){//市级
            ssqxMap.put("", "请选择");
            for(Item it:ssqxlist){
                ssqxMap.put(it.getKey(), it.getValue());
            }
            resultMap.put("ssqxMap", ssqxMap);
            return resultMap;
        }else{//
            String ssqxcode = user.getUnit().getSsqx();
            for(Item it:ssqxlist){
                if(it.getKey().equals(ssqxcode)){
                    ssqxMap.put(it.getKey(), it.getValue());
                    break;
                }
            }
            resultMap.put("ssqxMap", ssqxMap);
            return resultMap;
        }
    }

    //根据用户权限取得对应管辖范围
    public Map  getSsqxAndSchMapByUserPriv(UnitBasicinfoService mgr){
        UserDO user = getUser();
        Map resultMap = new HashMap();
        List<Item> ssqxlist = DicSet.get(DicUtil.SCHOOL.SCHOOL_SSQX);
        Map ssqxMap = new LinkedHashMap();
        //根据当前用户权限获取区县范围
        if(PermissUtil.hasPermission(PrivContent.SM_SJPRIV)){//市级
            ssqxMap.put("", "请选择");
            for(Item it:ssqxlist){
                ssqxMap.put(it.getKey(), it.getValue());
            }
            resultMap.put("ssqxMap", ssqxMap);
            Map schMap = UnitUtil.getSchMap(mgr,new HashMap(), true);
            resultMap.put("schMap", schMap);
            return resultMap;
        }else if(PermissUtil.hasPermission(PrivContent.SM_QJPRIV)){// 区级
            String ssqxcode = user.getUnit().getSsqx();
            for(Item it:ssqxlist){
                if(it.getKey().equals(ssqxcode)){
                    ssqxMap.put(it.getKey(), it.getValue());
                    break;
                }
            }
            resultMap.put("ssqxMap", ssqxMap);
            Map params = new HashMap();
            params.put("ssqx", ssqxcode);
            Map schMap = UnitUtil.getSchMap(mgr,params, true);
            schMap.put("", "请选择");
            resultMap.put("schMap", schMap);
            return resultMap;
        }else{
            String ssqxcode = user.getUnit().getSsqx();
            for(Item it:ssqxlist){
                if(it.getKey().equals(ssqxcode)){
                    ssqxMap.put(it.getKey(), it.getValue());
                    break;
                }
            }
            resultMap.put("ssqxMap", ssqxMap);
            Map schMap = new HashMap();
            schMap.put(user.getUnit().getId(),user.getUnit().getXxmc());
            resultMap.put("schMap", schMap);
            return resultMap;
        }
    }

}